<?php
App::uses('AppController', 'Controller');
/**
 * Professors Controller
 *
 * @property Professor $Professor
 * @property PaginatorComponent $Paginator
 */
class ProfessorsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Professor->recursive = 0;
		$this->set('professors', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Professor->exists($id)) {
			throw new NotFoundException(__('Invalid professor'));
		}
		$options = array('conditions' => array('Professor.' . $this->Professor->primaryKey => $id));
		$this->set('professor', $this->Professor->find('first', $options));
	}

/**
 * add method
 * Modificada desde bakeado
 * @return void
 */
	public function add($seccionId = null) {
		if ($this->request->is('post')) {
			$this->Professor->create();
			$profeExiste = $this->Professor->find('all', array('conditions' => array('section_id' => $this->request->data['Professor']['section_id'], 'user_id' => $this->request->data['Professor']['user_id'])));
			
			if(sizeof($profeExiste) >0){
				$this->Session->setFlash('Profesor ya se encuentra asociado a la sección.');
				return $this->redirect(array('action'=>'add', $this->request->data['Professor']['section_id']));
			}
			if ($this->Professor->save($this->request->data)) {
				$this->Session->setFlash(__('Profesor agregado con exito.'));
				return $this->redirect(array('controller' => 'Sections', 'action' => 'viewProfessor', $this->request->data['Professor']['section_id']));
			} else {
				$this->Session->setFlash(__('The professor could not be saved. Please, try again.'));
			}
		}
		$users = $this->Professor->User->find('list');
		$this->set('users', $users);
		$this->set('seccion', $this->Professor->Section->findById($seccionId));
	}


/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
//Modificado desde bake
	public function delete($id = null) {
		$this->Professor->id = $id;
		if (!$this->Professor->exists()) {
			throw new NotFoundException(__('Invalid professor'));
		}
		$this->request->onlyAllow('post', 'delete');
		$section_id = $this->Professor->findById($id)['Professor']['section_id'];
		if ($this->Professor->delete()) {
			$this->Session->setFlash(__('El profesor ha sido desvinculado de la sección con exito.'));
		} else {
			$this->Session->setFlash(__('El profesor no ha podido ser desvinculado. Por favor, intentelo nuevamente.'));
		}
		
		$this->redirect(array('controller' => 'Sections', 'action' => 'viewProfessor', $section_id));
	}




	public function buscarIdProfesor($name){
		$nombre = explode(' ', $name);
		
		if(sizeof($nombre) == 2){
			if(!($p = $this->Professor->User->find('all', array(
				'conditions' => array(
					'first_name' => $nombre[0],
					'last_name' => $nombre[1]))))){
				debug($this->Professor->User->validationErrors);
				return false;
			}
				
		}else{
			if(!($p = $this->Professor->User->find('all', array(
				'conditions' => array(
					'OR', array(
						array('first_name' => $nombre[0].' '.$nombre[1],
						'last_name' => $nombre[2]), 
						array('first_name' => $nombre[0],
						'last_name' => $nombre[1].' '.$nombre[2])
					)
				)
			)))){
				debug($this->Professor->User->validationErrors);
				return false;
			}				
		}		
		return $p;			
	}

	public function buscarProfesor($idSection){
		return $this->Professor->find('all', array(
			'conditions' => array( 
				'section_id' => $idSection)));


	}

	public function addExcel($data){
		$this->Professor->create();
		if(!$this->Professor->save($data)){
			debug($this->Professor->validationErrors);
			return false;
		}
	}

	public function deleteExcel($id){
		$this->Professor->id = $id;
		$this->Professor->delete();
	}

	
}
?>
